﻿Imports System.Threading

Public Class Form1

    Dim xulie_number As String
    Dim receive_buffer(17) As Byte



    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For i = 0 To 10
            icdev1 = Common.rf_init(i, 115200)
            Common.rf_beep(icdev1, 300)
            If icdev1 >= 0 Then

                '找到读卡器就停止循环
                Exit For
            End If
        Next


    End Sub






    ''' <summary>
    ''' 寻卡
    ''' </summary>
    ''' <param name="icdev"></param>
    ''' <returns></returns>
    Private Function Find_card(ByVal icdev As Integer) As String
        Dim tagtype As UInt16 = 0
        Dim size As Byte = 0
        Dim snr As UInteger = 0
        Dim st As Short
        mifareone.rf_reset(icdev, 3)
        st = mifareone.rf_request(icdev, 1, tagtype)
        If st <> 0 Then
            Return 0 '"request error!"
        End If

        st = mifareone.rf_anticoll(icdev, 0, snr)
        If st <> 0 Then
            Return 1 '"anticoll error!"
        End If
        Dim snrstr As String = ""
        snrstr = snr.ToString("X")
        st = mifareone.rf_select(icdev, snr, size)
        If st <> 0 Then
            Return 2 '"select error!"
        End If
        Common.rf_beep(icdev, 10)

        Return snrstr
    End Function

    Dim icdev1 As Integer


    ''' <summary>
    '''获取卡号
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click



        For i = 0 To 10
            icdev1 = Common.rf_init(i, 115200)
            Common.rf_beep(icdev1, 300)
            If icdev1 >= 0 Then

                '找到读卡器就停止循环
                Exit For
            End If
        Next


        Dim a = Common.rf_srd_snr(icdev1, 16, receive_buffer)
        xulie_number = ""
        For index = 0 To receive_buffer.Length - 1
            xulie_number += receive_buffer(index).ToString()
        Next

        TextBox1.Text = xulie_number


        'Button2.Enabled = False
        'Dim th As New Thread(Sub()
        '                         Try

        '                             While True
        '                                 Thread.Sleep(1000)
        '                                 Me.Invoke(New Action(Sub()

        '                                                          'SendKeys.SendWait(xulie_number)
        '                                                          'SendKeys.SendWait("{ENTER}")
        '                                                          TextBox1.Text = xulie_number
        '                                                      End Sub))

        '                                 'Dim card As String = Find_card(icdev1)
        '                                 'Select Case card
        '                                 '    Case 0, 1, 2
        '                                 '    Case Else

        '                                 '        SendKeys.SendWait(card)
        '                                 '        SendKeys.SendWait("{ENTER}")

        '                                 'End Select

        '                             End While
        '                         Catch ex As Exception

        '                         End Try


        '                     End Sub)

        'th.Start()
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs)
        System.Environment.Exit(0)
    End Sub

End Class
